﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Util;
using AkasaProject.Service;
using AkasaProject.Model;
using AkasaProject.DB;

namespace NoteJs
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private string pub_Type = "";
        private string pub_Name = "";
        private MainService service = new MainService();


        public MainWindow()
        {
            InitializeComponent();
            bindData();
        }
        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            MainModel mm = initdata();
            string where = MainDB.M_name+"='"+mm.m_name+"'";
            int rs = service.update(mm, where);
            if (rs > 0)
            {
                MessageBoxResult result = MessageBox.Show("确定是退出吗？", "数据已保存", MessageBoxButton.YesNo, MessageBoxImage.Question);
                //关闭窗口
                if (result == MessageBoxResult.Yes)
                    e.Cancel = false;

                //不关闭窗口
                if (result == MessageBoxResult.No)
                    e.Cancel = true;
            }
        }

        public DataSet getData(string sql)
        {
            DataSet dt = SQLHelper.Query(sql);
            return dt;
        }
        public DataSet getDataByType(string type = "")
        {
            string where = string.Empty;
            switch (type)
            {
                case "-1":
                    where = " "+MainDB.Type+"='" + type + "'";
                    break;
                case "0":
                    where = " "+MainDB.Type+"='" + type + "'";
                    break;
                case "1":
                    where = " "+MainDB.Type+"='" + type + "'";
                    break;
                default:
                    where = " "+MainDB.Type+"='1'";
                    break;

            }
            return service.select(where);
        }
        /// <summary>
        /// 初始化数据列表
        /// </summary>
        void bindData()
        {
            pub_Type = "1";
            if (getDataByType() != null && getDataByType().Tables[0].Rows.Count > 0)
                pub_Name = getDataByType().Tables[0].Rows[0][MainDB.M_name].ToString();
            updateText(getDataByType());
            updateList(getDataByType());
        }
        /// <summary>
        /// 更新数据列表
        /// </summary>
        void changeData(string type, string name = "")
        {
            if (!string.IsNullOrEmpty(name))
            {
                string where = " "+MainDB.M_name+"='" + name + "'";
                updateText(service.select(where));
            }
            else
            {
                updateList(getDataByType(type));
                updateText(getDataByType(type));
            }
        }
        /// <summary>
        /// 更新需求、进度等内容
        /// </summary>
        /// <param name="ds"></param>
        private void updateText(DataSet ds)
        {
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
               
                biji.Text = ds.Tables[0].Rows[0][MainDB.NoteText].ToString();
                jindu.Text = ds.Tables[0].Rows[0][MainDB.JinduText].ToString();
                xuqiu.Text = ds.Tables[0].Rows[0][MainDB.XuqiuText].ToString();
                ceshi.Text = ds.Tables[0].Rows[0][MainDB.CeshiText].ToString();
                yanshou.Text = ds.Tables[0].Rows[0][MainDB.YanshouText].ToString();
                fujian.Text = ds.Tables[0].Rows[0][MainDB.FujianText].ToString();
            }
            else
            {
                biji.Text = "";
                jindu.Text = "";
                xuqiu.Text = "";
                ceshi.Text = "";
                yanshou.Text = "";
                fujian.Text = "";

            }
        }
        /// <summary>
        /// 更新右侧的项目列表
        /// </summary>
        /// <param name="ds"></param>
        private void updateList(DataSet ds)
        {
            ListPro.Items.Clear();
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    ListPro.Items.Add(ds.Tables[0].Rows[i]["m_name"].ToString());

                }
                ListPro.SelectedIndex = 0;
            }
        }
        /// <summary>
        /// 新增项目
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            string pName = inputName.Text;
            DateTime cTime = DateTime.Now;
            if (string.IsNullOrEmpty(pName) || pName == "请输入项目名称")
            {
                MessageBox.Show("请输入项目名称!");

            }
            else
            {
                string where = " " + MainDB.M_name + "='" + pName + "'";
                int count = service.select(where).Tables[0].Rows.Count;
                if (count > 1)
                {
                    MessageBox.Show("项目已存在！");
                }
                else
                {
                    string sql = "insert into Main (m_name,createdate,type) values ('" + pName + "','" + cTime + "',1)";
                    MainModel mm = new MainModel();
                    mm.m_name = pName;
                    mm.createdate = cTime.ToString();
                    mm.type = "1";
                    int rs = service.insert(mm);
                    if (rs > 0)
                    {
                        MessageBox.Show("添加项目成功！");
                        updateText(getDataByType());
                        updateList(getDataByType());
                        inputName.Text = "请输入项目名称!";
                    }
                    else
                    {
                        MessageBox.Show("添加项目失败！");
                    }
                }
            }
        }


       

        /// <summary>
        /// 设置项目成功
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Set_Success(object sender, RoutedEventArgs e)
        {
            string currTime = DateTime.Now.ToString();
            string sets = " "+MainDB.Type+"='0',"+MainDB.ChangeDatte+"='" + currTime+"'";
            string where = MainDB.M_name + "='" + pub_Name + "'";
            int rs = service.updateBySets(sets,where);
            MessageBox.Show("设置成功！" + service.updateBySets(sets, where));
            changeData(pub_Type);
        }
        /// <summary>
        /// 设置项目失效
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Set_Failed(object sender, RoutedEventArgs e)
        {
            string currTime = DateTime.Now.ToString();
            string sets = " " + MainDB.Type + "='-1'," + MainDB.ChangeDatte + "='" + currTime + "'";
            string where = MainDB.M_name + "='" + pub_Name + "'";
            MessageBox.Show("设置成功！" + service.updateBySets(sets, where));
            changeData(pub_Type);
        }
        /// <summary>
        /// 更新当前项目需求，进度等内容
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Update_Text(object sender, RoutedEventArgs e)
        {
            MainModel mm = initdata();
            string where = MainDB.M_name + "='" + mm.m_name + "'";
            int rs = service.update(mm, where);
            if (rs > 0)

                MessageBox.Show("更新成功！" + service.update(mm,where));
            else
                MessageBox.Show("更新失败！");
        }
        /// <summary>
        /// 初始化当前数据。返回MainModel数据
        /// </summary>
        /// <returns></returns>
        private MainModel initdata() {
            MainModel mm = new MainModel();
            string currTime = DateTime.Now.ToString();
            mm.noteText = biji.Text;
            mm.jinduText = jindu.Text;
            mm.xuqiuText = xuqiu.Text;
            mm.ceshiText = ceshi.Text;
            mm.ceshiText = ceshi.Text;
            mm.yanshouText = yanshou.Text;
            mm.fujianText = fujian.Text;
            mm.changedate = currTime;
            mm.m_name = pub_Name;
            return mm;
        }
        /// <summary>
        /// 右侧的列表点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ListPro_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {
            object p = ListPro.SelectedItem;
            if (p == null)
                return;
            string m_name = p.ToString();
            pub_Name = p.ToString();
            changeData(pub_Type, m_name);
        }


        #region inputName事件
        private void inputName_MouseLeave(object sender, MouseEventArgs e)
        {
            inputName.Text = "请输入项目名称";
        }

        private void inputName_MouseEnter(object sender, MouseEventArgs e)
        {
            inputName.Text = "";
        }
        #endregion

        #region Runing Success Droping 鼠标激活状态
        /// <summary>
        /// 查看当前项目
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Runing_Click(object sender, RoutedEventArgs e)
        {
            pub_Type = "1";
            changeData(pub_Type);
        }
        /// <summary>
        /// 查看成功的项目
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Success_Click(object sender, RoutedEventArgs e)
        {
            pub_Type = "0";
            changeData(pub_Type);
         
        }
        /// <summary>
        /// 查看失效的项目
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Failed_Click(object sender, RoutedEventArgs e)
        {
            pub_Type = "-1";
            changeData(pub_Type);
          
        }

        private void initButton() {

        }
        #endregion

    }
}
